package ru.averta.notes.service.search.bridge;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.LuceneOptions;
import ru.averta.notes.dao.impl.DaoFactory;
import ru.averta.notes.domain.Site;
import ru.averta.notes.domain.TagConnector;

import java.util.Collection;

/**
 * <code>TagFieldBridge</code>
 * <p/>
 * Осуществляет индексацию тегов у Site'а
 *
 * @author fedor.belov (fedor.belov@mail.ru)
 *         Date: 16.09.11 16:17
 */
public class TagFieldBridge implements FieldBridge {

    @Override
    public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {

        if (!(value instanceof Site)) {
            throw new IllegalArgumentException();
        }

        Site site = (Site) value;
        Collection<TagConnector> connectors = DaoFactory.getTagConnectorDao().findByObject(site);

        for (TagConnector connector : connectors) {
            Field field = new Field("tagConnectors.tag.title",
                    connector.getTag().getTitle(),
                    luceneOptions.getStore(),
                    Field.Index.NOT_ANALYZED,
                    luceneOptions.getTermVector());

            document.add(field);
        }
    }
}
